package com.ruoyi.kong.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.kong.entity.KongEntity;
import com.ruoyi.kong.entity.KongPlugin;
import com.ruoyi.kong.entity.KongPluginVo;
import com.ruoyi.kong.service.IKongPluginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * kong 路由 控制类
 */
@RestController
@RequestMapping("/kong/plugins")
public class KongPluginsController extends BaseController {

    @Autowired
    private IKongPluginInfoService kongPluginInfoService;

    @GetMapping("/list")
    public TableDataInfo getPluginList(){
        startPage();
        KongEntity<KongPlugin> plugins = kongPluginInfoService.plugins();
        return  getDataTable(plugins.getData());
    }

    @GetMapping("/route/{routeId}")
    public TableDataInfo getPluginsByRouteId(@PathVariable String routeId){
        startPage();
        KongEntity<KongPlugin> kongPluginKongEntity = kongPluginInfoService.pluginsByRouteId(routeId);
        return getDataTable(kongPluginKongEntity.getData());
    }

    @GetMapping("/service/{serviceId}")
    public TableDataInfo getPluginsByServiceId(@PathVariable String serviceId){
        startPage();
        KongEntity<KongPlugin> kongPluginKongEntity = kongPluginInfoService.pluginsByServiceId(serviceId);
        return getDataTable(kongPluginKongEntity.getData());
    }

    @GetMapping("/schema/{name}")
    public AjaxResult getPluginSchemaByName(@PathVariable  String name){
        Map schemaMap = kongPluginInfoService.pluginSchemaByName(name);
        return AjaxResult.success(schemaMap);
    }

    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody KongPlugin kongPlugin){
        KongEntity<KongPlugin> _kongPlugin = kongPluginInfoService.addPlugin(kongPlugin);
        return AjaxResult.success(_kongPlugin.getData());
    }

    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable String id){
        kongPluginInfoService.deletePlugin(id);
        return success();
    }

    @PostMapping("/update/{id}")
    public AjaxResult update(@PathVariable String id, @RequestBody KongPluginVo kongPlugin){
        KongPlugin _kongPlugin = kongPluginInfoService.updatePlugin(id, kongPlugin);
        return  AjaxResult.success(_kongPlugin);
    }



}
